FROM httpd

MAINTAINER phithon <root@leavesongs.com>

WORKDIR /tmp

ENV COMMON_TOOLS \
    wget \
    curl \
    libsqlite3-0 \
    ca-certificates \
    openssl \
    pkg-config

ENV BUILD_TOOLS \
    $COMMON_TOOLS \
    build-essential \
    bison \
    autoconf \
    libcurl4-openssl-dev \
    libsqlite3-dev \
    libssl-dev=$OPENSSL_VERSION

ENV PHP_INI_DIR /usr/local/etc/php
RUN mkdir -p $PHP_INI_DIR/conf.d

RUN apt-get update \
    && apt-get -y install $BUILD_TOOLS \
    && rm -rf /var/lib/apt/lists/*

RUN mkdir -p /usr/src/php \
    && wget -qO- http://museum.php.net/php5/php-5.4.1.tar.gz | tar zx -C /usr/src/php --strip-components=1 \
    && cd /usr/src/php \
    && ./configure \
		--with-config-file-path="$PHP_INI_DIR" \
		--with-config-file-scan-dir="$PHP_INI_DIR/conf.d" \
		\
        --without-pear \
        --disable-libxml \
        --disable-dom \
        --disable-simplexml \
        --disable-xml \
        --disable-xmlreader \
        --disable-xmlwriter \
	&& make -j "$(nproc)" \
	&& make install \
	&& { find /usr/local/bin /usr/local/sbin -type f -executable -exec strip --strip-all '{}' + || true; } \
	&& make clean

RUN apt-get purge -y --auto-remove $BUILD_TOOLS \
    && rm ${HTTPD_PREFIX}/htdocs/index.html \
    && rm -rf /usr/src/php

RUN { \
        echo; \
        echo '<Directory "/usr/local/bin">'; \
        echo '  Require all granted'; \
        echo '  Options ExecCGI'; \
        echo '</Directory>'; \
        echo; \
        echo 'ScriptAlias /local-bin /usr/local/bin'; \
        echo; \
        echo "<Directory \"${HTTPD_PREFIX}/htdocs\">"; \
        echo '  AddHandler application/x-httpd-php5 php'; \
        echo '  Action application/x-httpd-php5 /local-bin/php-cgi'; \
        echo '  Options -Indexes'; \
        echo '  Require all granted'; \
        echo '</Directory>'; \
        echo; \
    } >> ${HTTPD_PREFIX}/conf/httpd.conf \
    && sed -i -e "s/\#LoadModule cgi_module/LoadModule cgi_module/g" ${HTTPD_PREFIX}/conf/httpd.conf \
    && sed -i -e "s/\#LoadModule cgid_module/LoadModule cgid_module/g" ${HTTPD_PREFIX}/conf/httpd.conf \
    && sed -i -e "s/\#LoadModule actions_module/LoadModule actions_module/g" ${HTTPD_PREFIX}/conf/httpd.conf \
    && sed -i -e "s/DirectoryIndex index\.html/DirectoryIndex index.html index.php/g" ${HTTPD_PREFIX}/conf/httpd.conf